<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-11-12 20:15:56
 * @LastEditTime: 2021-02-09 18:37:36
 * @LastEditors: 橘子味的心
 * @Reference:
 */
namespace app\system\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\View;
use think\helper\Str;

class Base extends BaseController
{
    public function initialize()
    {
        $session = session('auth_user');
        if (!isset($session)) {
            $this->error('检测到用户未登录，请先登录', url('login/index'), ['top' => true]);
        }
        // $name = request()->url();
        $name = request()->controller() . '/' . request()->action();

        // $name = explode('.', $name)[0];
        // dump($name);
        $name = implode('/', array_filter(explode('/', $name)));
        // dd($name);
        $notCheck = [
            'Index/index'
            , 'Index/menu'
            , 'Index/welcome'
            , 'AuthUser/lst'
            , 'AuthUser/logout'
            , 'Login/logout',
        ];
        $auth = new Auth();
        if ($session['id'] != 1) {
            if (!in_array($name, $notCheck)) {
                if (!$auth->auth_check($name, $session['id'])) {
                    return tocode(101); //没有权限!
                }
            }
        }
        $rule = cache('rules');
        if (!$rule) {
            $tree = array_map('intval', explode(',', $session['rules']));
            $rule = $auth->getRule();
            $map[] = ['group_id', '=', $session['id']];
            $map[] = ['rule_id', 'in', $tree];
            foreach ($rule as &$k) {
                if (in_array($k['id'], $tree)) {
                    $k['checked'] = true;
                }
                $k['url'] = (empty($k['url']) ? "" : $this->lower($k['url']));
                $a = explode('/', $k['url']);
                array_pop($a);
                $k['child'] = $auth->get_rule_special($k['id'], true, implode('/', $a));
            }
            cache('rules', $rule);
        }
        $res = filter_by_value($rule, 'url', $this->lower($name), true);
        if (!empty(input('check'))) {
            return tocode(200, '有权限!', $res);
        }

        if (empty($res)) {
            return;
        }

        $breadcrumb[] = $res;
        if (!empty($res)) {
            $pid = $res['pid'];
            //获取上级
            while ($pid > 0) {
                if (!empty($res['pid'])) {
                    $attr = filter_by_value($rule, 'id', $pid);
                    if (isset($attr)) {
                        $breadcrumb[] = $attr;
                        if (isset($attr['pid'])) {
                            $pid = $attr['pid'];
                        } else {
                            break;
                        }
                    }
                }
            }
            if (isset($breadcrumb[1])) {
                $breadcrumb[0]['title'] = $breadcrumb[1]['title'] . $breadcrumb[0]['title'];
            }
        }
        View::assign('breadcrumb', array_reverse($breadcrumb));
    }
    public function lower($str)
    {
        $data = [];
        foreach (explode('/', $str) as $v) {
            $data[] = (empty($v) ? "" : Str::lower(Str::snake($v)));
        }
        return (empty($data) ? "" : implode('/', $data));
    }
}
